package com.hengda.museumonline.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.BaseLoadMoreHeaderAdapter;
import com.hengda.museumonline.adapter.BaseViewHolder;
import com.hengda.museumonline.adapter.ErrorAdapter;
import com.hengda.museumonline.entity.NoticeBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.http.GetData;
import com.hengda.museumonline.ui.NewsContentActivity;
import com.hengda.museumonline.ui.MainActivity;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.List;

/**
 * @author：JFWU on 2016/9/26 20:29
 * @E-mail：jeffy12138@126.com
 */
public class NoticeFragment extends Fragment {
    private FrameLayout fl_content;
    private RecyclerView recyclerView;
    private ImageView loading_bg;
    private NoticeBean noticeBean2 = new NoticeBean();
    private NoticeBean noticeBean1 = new NoticeBean();
    private MyAdapter mAdapter;
    private int i = 1;

    public NoticeFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fl_content = (FrameLayout) inflater.inflate(R.layout.fragment_notice, container, false);
        loading_bg = (ImageView) fl_content.findViewById(R.id.loading_bg);
        loading_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "我被点击了", Toast.LENGTH_SHORT).show();
                System.out.println("我被点击了");
            }
        });
        recyclerView = (RecyclerView) fl_content.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        getDataFromServer();
        return fl_content;
    }

    private void getDataFromServer() {
        Request request = GetData.getNews(getContext(), "http://192.168.10.20/wbzxapi/news/news", "a", "1.0", 6, 0, 20);
        CallServer.getRequestInstance().add(getContext(), 0, request, listener);
    }

    OnResponseListener listener = new OnResponseListener() {
        @Override
        public void onStart(int what) {

        }

        @Override
        public void onSucceed(int what, Response response) {
            int responseCode = response.getHeaders().getResponseCode();// 服务器响应码
            Gson gson = new Gson();
            if (0 == what) {
                System.out.println("结果：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + response.get().toString());
                noticeBean1 = gson.fromJson(response.get().toString(), NoticeBean.class);
                if (noticeBean1 != null && noticeBean1.getData() != null && noticeBean1.getData().size() > 0) {
                    loading_bg.setVisibility(View.INVISIBLE);
                    mAdapter = new MyAdapter(getContext(), recyclerView, noticeBean1.getData(), R.layout.notice_item);
                    recyclerView.setAdapter(mAdapter);
                    mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
                        @Override
                        public void onLoadMore() {
                            Request request = GetData.getNews(getContext(), "http://192.168.10.20/wbzxapi/news/news", "a", "1.0", 6, 20 * i, 20);
                            i += 1;
                            CallServer.getRequestInstance().add(getContext(), 1, request, listener);
                        }
                    });
                } else {
                    recyclerView.setAdapter(new ErrorAdapter(getContext()));
                }
            } else if (1 == what) {
                noticeBean2 = gson.fromJson(response.get().toString(), NoticeBean.class);
                if (noticeBean2.getData() == null) {
                    mAdapter.setLoading(false);
                } else {
                    mAdapter.addAll(noticeBean2.getData());
                }
            }
        }

        @Override
        public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
            System.out.println("失败：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + exception);
            recyclerView.setAdapter(new ErrorAdapter(getContext()));
        }

        @Override
        public void onFinish(int what) {

        }
    };


    class MyAdapter extends BaseLoadMoreHeaderAdapter<NoticeBean.DataBean> {

        public MyAdapter(Context mContext, RecyclerView recyclerView, List<NoticeBean.DataBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }


        @Override
        public void convert(final Context mContext, RecyclerView.ViewHolder holder, final NoticeBean.DataBean dataBean) {
            if ("9".equals(dataBean.getTerm_id())) {
                ((BaseViewHolder) holder).setText(R.id.term_id, "预告");
                ((BaseViewHolder) holder).setVisible(R.id.yugao, true);
                ((BaseViewHolder) holder).setVisible(R.id.gonggao, false);
                ((BaseViewHolder) holder).setText(R.id.post_modified, dataBean.getPost_modified());
                ((BaseViewHolder) holder).setText(R.id.post_source, dataBean.getPost_source());
            } else if ("10".equals(dataBean.getTerm_id())) {
                ((BaseViewHolder) holder).setText(R.id.term_id, "公告");
                ((BaseViewHolder) holder).setVisible(R.id.yugao, false);
                ((BaseViewHolder) holder).setVisible(R.id.gonggao, true);
                ((BaseViewHolder) holder).setText(R.id.post_modified1, dataBean.getPost_modified().substring(0, 10));
                ((BaseViewHolder) holder).setText(R.id.post_source1, dataBean.getPost_source());
            }
            ((BaseViewHolder) holder).setText(R.id.post_title, dataBean.getPost_title());
            ((BaseViewHolder) holder).setImageUrl(R.id.img, dataBean.getImg(), R.mipmap.cheese_1);
            ((BaseViewHolder) holder).setTag(R.id.post_title, dataBean.getObject_id());
            ((BaseViewHolder) holder).setOnClickListener(R.id.notice_item, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(mContext, dataBean.getPost_title(), Toast.LENGTH_SHORT).show();
                    Bundle bundle = new Bundle();
                    bundle.putString("object_id", dataBean.getObject_id());
                 //   mainActivity.startActivity(mContext, NewsContentActivity.class, bundle);
                }
            });
        }
    }


}
